{% macro selectinputdict(name, values, value='') -%}
    <select name="{{ name }}" id="{{ name }}">
    {% for k,v in values.items() %}
        <option value="{{ k }}"  {{ 'selected' if value==k }}>{{ v }}</option>
    {% endfor %}
    </select>
{%- endmacro %}

{% extends "layout.html" %}


{% block body %}
<style>
.table-condensed tr td {padding-top: 0px  !important;padding-bottom: 0px  !important; } {# On condense encore plus #}
#TblTaxo td:nth-child(1) {font-size: small;padding: 0}
</style>
  {{ PreviousTxt|safe}}
<h3>自动分类：设置 </h3>
<form class="form-horizontal" method="post" >
<input type="hidden" id="learninglimit" name="learninglimit" value="{{ data.learninglimit }}">
<input type="hidden" id="Taxo" name="Taxo" value="{{ data.Taxo }}">
<input type="hidden" id="src" name="src" value="{{ data.src }}">
<input type="hidden" id="PostTaxoMapping" name="PostTaxoMapping" value="{{ data.PostTaxoMapping }}">

<div class="row">
  <div class="col-md-4">

<div class="form-group">
    <label class="col-sm-6 control-label" >记录先前的自动分类</label>
    <div class="col-sm-4">
        {{ selectinputdict("keeplog",{'no':"否",'yes':"是"},"no") }}
    </div> <!-- col-sm-4 -->
  </div><!-- /form-group -->

      <p style="padding-left: 100px;">
    <button type="submit" class="btn btn-primary">训练和预测识别</button>
        </p>

  </div>  <div class="col-md-8">
<div class="panel  panel-default">    <ul>
<li>预测（自动分类）永远不能取代人工验证
<li>分类的默认设置已经过专家验证。在大多数情况下，用户应该保留它们
<li>分类设置记录在Ecotaxa中，以便下一次预测时使用
<li>用于预测的类别应该由专家准确地验证过
<li>经验表明，自动分类为有限数量的类别，然后使用更多类别进行详细验证通常会更有效
</ul></div>
</div>
</div> {#ROW#}

<script>
    function ComputeCritVar() {
        var cv="";
        $('#TblVar input').each(function (){
           if($(this).prop("checked")){
               if(cv!="")
                   cv+=",";
               cv+=$(this).data("var");
           }
        });
        $('#CritVar').val(cv);
    }
    function CheckBasedOnText() {
        var cv=$('#CritVar').val();
        var cvl=cv.split(",");
        //console.log(cvl);
        $('#TblVar input').each(function (){
           if($(this).prop("checked",cvl.indexOf($(this).data("var"))>=0)){
               if(cv!="")
                   cv+=",";
               cv+=$(this).data("var");
           }
        });
    }
    function UncheckNonPertinent(){
        var perimetre=$('#Perimeter').val();
        $('#TblVar tr').each(function (){
           var v=parseInt($(this).find("td:nth-child(3)").text() ) ;
           var checkit=true;
           if((isNaN(v))||(v>=25)) {# Clean si moins de 25% de données sources remplis #}
               checkit=false;
           v=parseInt($(this).find("td:nth-child(5)").text() );
           if((isNaN(v))||(v>=25)) {#  Clean si moins de 25% de données cibles remplis #}
               checkit=false;
           $(this).find("input").prop("checked",checkit);
        });
        ComputeCritVar();
    }
</script>

<table>
<tr><td valign="top">
{# ----------- Partie Gestion des Variable --------------------- #}
使用变量 :<br>
<input type="text" name="CritVar" id="CritVar" value="{{ data.CritVar }}" style="width: 400px;display: None">
选择 <a name="tbltop2" href="#tbltop2" onclick="$('#TblVar input').prop( 'checked', true );ComputeCritVar();return false;">所有</a>
    / <a href="#tbltop2" onclick="UncheckNonPertinent();return false;" title="uncheck missing %>=25%" >最相关</a>
{#    / <a href="#tbltop2" onclick="CheckBasedOnText();return false;" title="Usefull to copy setting from another project" >Text field content</a>#}
    / <a href="#tbltop2" onclick="$('#TblVar input').prop( 'checked', false );ComputeCritVar();return false;">不选</a>
 - <a href="#variable-help" data-toggle="collapse" class="help">变量解释</a>
<div id=variable-help class="panel panel-default collapse " style="margin-left: 0px;margin-top: 5px;width: 500px" >
<div style="margin:5px;">
<span class=" glyphicon glyphicon-info-sign"></span> <ul><li>从训练数据和分类数据中随机抽取50000个对象进行统计：对于每个特征，计算不同值的数量和缺失值的百分比。
</li><li>具有单一、常数值（即无方差==> Distinct=N）的特征对于分类是无用的，会自动排除。
</li><li>值丢失超过25%的特征可能没有帮助，在选择“最相关”的特征时被排除在外。
</li><li>缺失的值由学习集中该特性的中值代替
</li></ul>
</div>
</div>

<table class="table table-bordered table-condensed" style="width: auto" id="TblVar">
    <tr><th rowspan="2">选择</th>
        <th width="200" rowspan="2" style="text-align: center;">Object variable<br><span style="font-weight: normal">Statistics on 50&nbsp;000 objects</span></th>
        <th width="100" colspan="2" style="text-align: center;line-height: 1;">Learning set </th>
        <th width="100" colspan="2" style="text-align: center;">Objects to predict</th></tr>
    <tr><th >% missing</th><th >Distinct</th><th >% missing</th><th>Distinct</th></tr>
    {%   for r in g.critlist %}
        <tr><td> {% if (r[2]=="Y") %}
            <input type="checkbox" value="Y" name="var{{ r[0] }}" data-var="{{ r[0] }}" >
            {% endif %}
        </td><td>{{ r[0] }}</td>
        <td class="rightfixedfont">{{ r[1] }}</td><td class="rightfixedfont">{{ r[2] }}</td><td class="rightfixedfont">{{ r[3] }}</td><td class="rightfixedfont">{{ r[4] }}</td></tr>
    {% endfor %}
</table>
</td><td style="width: 20px"></td><td valign="top">
  {% if g.SCN %}
  Use Deep Learning features :
    {% if g.SCNImpossible %}
      <div class="bg-warning"><span class="glyphicon glyphicon-exclamation-sign"></span> Not Available : {{ g.SCNImpossible }}</div>
      {% else %}
    <input type="checkbox" name="usescn" id="usescn" value="Y">
      {% endif %}
  <br>This functionnality require availaibility of SCN/Deep learning features
<style>
#TblScn td:nth-child(1) {max-width: 300px;}
</style>

  <table class="table table-bordered table-condensed" style="width: auto" id="TblScn">
    <tr>
      <th>Project</th>
      <th># Obj </th>
      <th># Missing SCN Features</th>
      <th>SCN Network</th>
    </tr>
  {%   for r in g.SCN %}
    <tr><td>{{ r['projid'] }} - {{ r['title'] }}</td>
        <td>{{ r['nbr'] }}</td><td>{{ r['miss_scn'] }}</td><td>{{ r['scnmodel'] }}</td>
    </tr>
  {% endfor %}
  </table>
  {% endif %}
</td></tr>
</table>
{# ----------- Partie Gestion des Taxon destination --------------------- #}
<a href="#TargetFilter" data-toggle="collapse" >
<span class="glyphicon glyphicon-plus-sign"></span> Advanced settings</a>
<div id=TargetFilter class="panel panel-default collapse " style="margin-left: 0px;margin-top: 5px;width: 800px;padding: 5px;" >

  OBJECTS to PREDICT {{ selectinputdict("Perimeter",{'nmc':"NOT Validated Objects (Unclassified or Predicted)",'all':"ALL Objects (for confusion matrix computation only)"},data.Perimeter) }}

<br>
{%  if g.LsSize >150000 %}
  <b>Save model :  </b><br>
  Model folder name <input type="text" name="savemodel_foldername" value="" autocomplete="off" style="width: 150px;">
  <br>Model Title <input type="text" name="savemodel_title" value="" autocomplete="off" style="width: 350px;">
  <br>Comments <textarea name="savemodel_comments" rows="2" cols="80"></textarea>
{% else %}
  <b>Not enought objects in the Learning Set to save the model</b>
{% endif %}
</div>

<div class="form-group">
  {{ label("Custom Settings <br>(Taxonomy Post Mapping)"|safe) }}
<div class="col-sm-4">
    <textarea  class="form-control" id="TxtCustSettings" name="TxtCustSettings"  rows="4">{{ g.TxtCustSettings }}</textarea>
</div>
<div class="col-sm-4">
    PostTaxoMapping=PredictecID:TargetId,PredictecID:TargetId,...
</div></div>

    <input type="hidden" name="starttask" value="Y">
    <div class="form-group">
    <div class="col-sm-offset-2 col-sm-2">
        <button type="submit" class="btn btn-primary">START Automatic PREDICTION of IDs</button>
    </div>
  </div>
</form>
   <script>
$(document).ready(function() {
        $('#TblVar').on('click','input',ComputeCritVar);
        if($('#CritVar').val()!=="")
          CheckBasedOnText();
        else {
          UncheckNonPertinent();
        }
    }); // Ready
 </script>
{% endblock %}